import request from '@/utils/request'
import { RoleRecords, RoleResponseData } from './type'
import { ResponseRoleMenu } from './type'

enum API {
  ROLEINFOLIMITS_URL = '/admin/acl/role/',
  ADDROLE_URL = '/admin/acl/role/save',
  UPDATEROLE_URL = '/admin/acl/role/update',
  MENUBYROLE_URL = '/admin/acl/permission/toAssign/',
  ADDUDPDATEACL_URL = '/admin/acl/permission/doAssign?',
  DELETEROLE_URL = '/admin/acl/role/remove/',
}

export const reqRoleInfoLimits = (
  page: number,
  limit: number,
  roleName?: string,
) =>
  request.get<any, RoleResponseData>(
    API.ROLEINFOLIMITS_URL + `${page}/${limit}`,
    { params: { roleName } },
  )
export const AddOrUpdateRole = (data: RoleRecords) => {
  if (data.id) {
    return request.put<any, any>(API.UPDATEROLE_URL, data)
  } else {
    return request.post<any, any>(API.ADDROLE_URL, data)
  }
}
export const getRoleMenuById = (roleId: number) =>
  request.get<any, ResponseRoleMenu>(API.MENUBYROLE_URL + roleId)
export const reqAddUpdateAcl = (roleId: number, checkArr: number[]) =>
  request.post<any, any>(
    API.ADDUDPDATEACL_URL + `roleId=${roleId}&permissionId=${checkArr}`,
  )
export const deleteCurrentRole = (id: number) =>
  request.delete<any, any>(API.DELETEROLE_URL + id)
